﻿<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>驰骋工作流</title>
    <!-- 引用通用的js文件. -->
    <script language="JavaScript" src="../Comm/JScript.js" type="text/javascript"></script>
    <script language="JavaScript" src="../Comm/JS/Calendar/WdatePicker.js" defer="defer"
            type="text/javascript"></script>
    <link href="../../DataUser/Style/ccbpm.css" rel="stylesheet" type="text/css" />
    <link rel="shortcut icon" href="../Img/ccbpm.ico" type="image/x-icon" />
    <script src="../Scripts/jquery-1.11.0.min.js" type="text/javascript"></script>
    <link href="../Scripts/bootstrap/css/bootstrap.css" rel="stylesheet" />

    <script src="../Scripts/bootstrap/js/bootstrap.min.js" type="text/javascript"></script>
    <script src="../Scripts/bootstrap/bootstrap-table/src/bootstrap-table.js" type="text/javascript"></script>
    <link href="../Scripts/bootstrap/bootstrap-table/src/bootstrap-table.css" rel="stylesheet" type="text/css" />
    <script src="../Scripts/bootstrap/bootstrap-table/src/locale/bootstrap-table-zh-CN.js" type="text/javascript"></script>

    <script src="../Scripts/bootstrap/bootstrap-treeview/src/js/bootstrap-treeview.js" type="text/javascript"></script>
    <link href="../Scripts/bootstrap/bootstrap-treeview/src/css/bootstrap-treeview.css" rel="stylesheet" type="text/css" />


    <!-- 导入配置文件. -->
    <script language="JavaScript" src="../Comm/JScript.js" type="text/javascript"></script>
    <script type="text/javascript" src="../Scripts/QueryString.js"></script>
    <script type="text/javascript" src="../Scripts/config.js"></script>
    <script type="text/javascript" src="../Comm/Gener.js"></script>
    <script type="text/javascript" language="javascript">
        var selectedRows = [];
        var subFlowGuid;
        $(function () {
            var subFlowMyPK = GetQueryString("SubFlowMyPK");
            subFlowGuid = new Entity("BP.WF.Template.SFlow.SubFlowHandGuide", subFlowMyPK);
            if (subFlowGuid.SubFlowGuideSQL == null || subFlowGuid.SubFlowGuideSQL == "") {
                alert("请配置批量启动子流程的实体数据源");
                return;
            }

            if (subFlowGuid.SubFlowStartModel == 1) {
                $("#treeDiv").hide();
                //列表批量启动模式
                ListSubFlowGuid(subFlowGuid);

            }
            if (subFlowGuid.SubFlowStartModel == 2) {
                $("#treeDiv").hide();
                //分组批量启动模式
                if (subFlowGuid.SubFlowGuideGroup == null || subFlowGuid.SubFlowGuideGroup == "") {
                    alert("请配置批量启动子流程的分组数据源")
                }
                GroupSubFlowGuid(subFlowGuid)
            }

            if (subFlowGuid.SubFlowStartModel == 3) {
                $("#listDiv").hide();
                //树结构启动模式
                if (subFlowGuid.SubFlowGuideSQL.indexOf("ParentNo") == -1) {
                    alert("配置批量启动子流程的SQL语句不包含ParentNo,请联系管理员");
                    return;
                }
                TreeSubFlowGuid(subFlowGuid);
            }
        });
        /**
         * 列表结构的解析
         * @param subFlowGuid
         */
        function ListSubFlowGuid(subFlowGuid) {
            var sql = subFlowGuid.SubFlowGuideSQL;
            sql = DealExp(sql);
            var dt = DBAccess.RunSQLReturnTable(sql);
            var columns = [];
            columns.push({
                field: "_checkbox",
                checkbox: true,
                formatter: function (value, row, index) {
                    if (row.checked) {
                        return {
                            "checked": true
                        }
                    }
                }

            });
            columns.push({
                field: 'No',
                title: '编号'
            });
            columns.push({
                field: 'Name',
                title: '名称'
            });

            var options = {
                striped: true,
                cache: false,
                strictSearch: true,
                minimumCountColumns: 2,
                highlightSelected: true,
                clickToSelect: true,
                singleSelect: false,
                sortable: false,
                cardView: false,
                detailView: false,
                uniqueId: "No",
                columns: columns
            };
            options.onCheck = function (row, element) {
                loadSelectGrid(row.No, row.Name);

            };
            options.onUncheck = function (row, element) {
                remove(row.No);
            };
            options.onCheckAll = function (rows) {
                selectedRows = [];
                rows.forEach(row => {
                    selectedRows.push({ No: row.No, Name: row.Name });
                });
            };
            options.onUnCheckAll = function (rows) {
                selectedRows = [];
            };
            $('#listGrid').bootstrapTable(options);
            $("#listGrid").bootstrapTable("load", dt);
        }
        /**
         * 分组结构的解析
         * @param subFlowGuid
         */
        function GroupSubFlowGuid(subFlowGuid) {
            //分组
            var sql = subFlowGuid.SubFlowGuideGroup;
            sql = DealExp(sql);
            var groupdt = DBAccess.RunSQLReturnTable(sql);
            //数据源
            sql = subFlowGuid.SubFlowGuideSQL;
            sql = DealExp(sql);
            var dt = DBAccess.RunSQLReturnTable(sql);
            var html = "";
            //获取分组字段
            var fieldFK = "", groupPK = "";
            if (groupdt.length != 0 && dt.length != 0) {
                var idx = 0;
                for (var obj in groupdt[0]) {
                    if (idx == 0) {
                        groupPK = obj;
                        break;
                    }
                    idx++;
                }

                idx = 0;
                for (var obj in dt[0]) {
                    if (idx == 2) {
                        fieldFK = obj;
                        break;
                    }
                    idx++;
                }
            }
            for (var i = 0; i < groupdt.length; i++) {
                var group = groupdt[i];

                html += "<tr>";
                html += "<th colspan=3>";
                html += "<input type='checkbox'  onclick='CheckAll(this.checked, \"" + group.No + "\");' id='Group_" + group.No + "'  /><label for='Group_" + group.No + "'>" + group.Name + "</label>";
                html += "</th>";
                html += "</tr>";
                var myNum = -1;
                for (var idx = 0; idx < dt.length; idx++) {
                    if (dt[fieldFK] != group[groupPK])
                        continue;

                    var isHave = false;

                    var lab = en.Name;
                    var checked = "";
                    if (isHave) {
                        lab = "<font color=green>" + en.Name + "</font>";
                        checked = "checked";
                    }

                    myNum++;
                    if (myNum == 0)
                        html += "<tr>";
                    html += "<td><input type='checkbox' name='" + group.No + "' id='CB_" + en.No + "' value='" + en.No + "' " + checked + " onclick='CheckOrUnCheckRow(this,\"" + en.No + "\",\"" + en.Name + "\")'/><label for='CB_" + en.No + "'>" + lab + "</label></td>";

                    if (myNum == showSpan - 1) {

                        html += "</tr>";
                        myNum = -1;
                    }
                }

                if (myNum == 0) {
                    html += "<td colspan=2></td>";
                    html += "</tr>";
                }

                if (myNum == 1) {
                    html += "<td></td>";
                    html += "</tr>";
                }

            }
            $("#listGrid").html(html);


        }
        /**
         * 树形结构的解析
         * @param subFlowGuid
         */
        function TreeSubFlowGuid(subFlowGuid) {
            //使用懒加载
            var sql = subFlowGuid.SubFlowGuideSQL.replace("@ParentNo", subFlowGuid.ParentNo);
            sql = DealExp(sql);
            var dt = DBAccess.RunSQLReturnTable(sql);
            var jsonTree = findChildren(dt, subFlowGuid.ParentNo);


            $('#tree').treeview({
                data: jsonTree,         // 数据源
                showCheckbox: false,   //是否显示复选框
                highlightSelected: true,    //是否高亮选中
                nodeIcon: 'glyphicon',
                multiSelect: false,    //多选

                onNodeChecked: function (event, data) {

                },
                onNodeSelected: function (event, data) {

                    loadSelectGrid(data.id, data.text);

                },
                onNodeExpanded: function (event, data) {
                    var url = sql = subFlowGuid.SubFlowGuideSQL.replace("@ParentNo", data.id);
                    var json = DBAccess.RunSQLReturnTable(url);
                    var jsonTree = findChildren(json, data.id);
                    $("#tree").treeview("deleteChildrenNode", data.nodeId);	//删除当前节点下的所有子节点

                    $("#tree").treeview("addNode", [data.nodeId, { node: jsonTree, silent: true }]);
                }
            });

            //显示已经选择的节点
            var gwfs = new Entities("BP.WF.GenerWorkFlows");

            var pworkid = GetQueryString("PWorkID")
            if (pworkid == null || pworkid == undefined)
                pworkid = 0;

            gwfs.Retrieve("PWorkID", pworkid, "FK_Flow", subFlowGuid.SubFlowNo, "WFState", 1);
            $.each(gwfs, function (i, item) {
                selectedRows.push({
                    "No": GetPara(item.AtPara, "SubFlowGuideEnNoFiled"),
                    "Name": GetPara(item.AtPara, "SubFlowGuideEnNameFiled"),
                    "WorkID": item.WorkID
                })
            });

            $('#selectGrid').bootstrapTable({
                striped: false,
                cache: false,
                data: selectedRows,
                sortOrder: "asc",
                strictSearch: true,
                minimumCountColumns: 2,
                clickToSelect: true,
                sortable: false,
                cardView: false,
                detailView: false,
                uniqueId: "No",
                columns: [{
                    field: 'No',
                    title: '编码',
                    visible: false
                }, {
                    field: 'Name',
                    title: '名称'
                }, {
                    field: 'WorkID',
                    title: 'WorkID',
                    visible: false
                }, {
                    title: '操作', formatter: function (value, row, index) {
                        return '<a class="btn btn-danger" href="#" onclick="remove(\'' + row.No + '\',' + row.WorkID + ')">移除</a>';
                    }
                }]
            });
            $('#selectGrid').bootstrapTable('hideColumn', 'No');
            var _Html = "<table>";
            _Html += '<thead class="">';
            _Html += '<tr>';
            _Html += '<th><label><input id="btSelectAll" type="checkbox" onclick="checkAllOrUncheck()"></th>';
            _Html += '<th style="width: 150px; ">编号</th >';
            _Html += '<th >名称</th >';
            _Html += "</tr>";
            _Html += '</thead>';
            _Html += "<tobdy>";

            for (var i = 0; i < dt.length; i++) {
                _Html += "<tr>";
                _Html += "<td><input type='checkbox'   id='" + dt[i].No + "' data-info='" + dt[i].Name + "'/></td><td>" + dt[i].Name + "</td><td>" + dt[i].No + "</td>";
                _Html += "</tr>"
            }
            _Html += "</tbody>";
            _Html += "</table>";
            $("#showMsg").html(_Html);

        }

        function findChildren(jsonArray, parentNo) {
            var appendToTree = function (treeToAppend, o) {
                $.each(treeToAppend, function (i, child) {
                    if (o.id == child.ParentNo)
                        o.nodes.push({
                            "id": child.No,
                            "text": child.Name,
                            "nodes": []
                        });
                });

                $.each(o.nodes, function (i, o) {
                    appendToTree(jsonArray, o);
                });

            };

            var jsonTree = [];
            var jsonchildTree = [];
            if ($.isArray(jsonArray) && typeof parentNo !== "undefined") {
                //如果parentNo不为0，则增加parentNo的部门数据
                if (parentNo != 0) {
                    $.each(jsonArray, function (i, item) {
                        if (item.No == parentNo) {
                            jsonTree.push({
                                "id": parentNo,
                                "text": item.Name,
                                "nodes": []
                            });
                            return;
                        }
                    });
                }


                $.each(jsonArray, function (i, o) {
                    if (o.ParentNo == parentNo) {
                        jsonchildTree.push(o);
                        jsonTree.push({
                            "id": o.No,
                            "text": o.Name,
                            "nodes": []
                        });
                    }
                });

                $.each(jsonTree, function (i, o) {
                    appendToTree(jsonArray, o);
                });

            }
            function _(treeArray) {
                $.each(treeArray, function (i, o) {
                    if ($.isArray(o.nodes)) {
                        if (o.nodes.length == 0) {
                            // o.nodes = undefined;
                        } else {
                            _(o.nodes);
                        }
                    }
                });
            }
            _(jsonTree);
            return jsonTree;
        }

        function loadSelectGrid(no, name) {
            if ($.isArray(selectedRows)) {
                var isHave = false;
                $.each(selectedRows, function (i, o) {
                    if (o.No == no) {
                        isHave = true;
                        return true;
                    }
                });
                if (isHave == false)
                    selectedRows.push({ "No": no, "Name": name });
            }
            if (subFlowGuid.SubFlowStartModel == 3)
                $('#selectGrid').bootstrapTable("load", selectedRows);
        }

        function remove(no) {
            if ($.isArray(selectedRows)) {
                var isHave = false;
                var objs = $.grep(selectedRows, function (o) {
                    return o.No != no;
                });
                selectedRows = objs;
            }
            if (subFlowGuid.SubFlowStartModel == 3)
                $('#selectGrid').bootstrapTable("load", selectedRows);
        }

        //选择与取消选择.
        function CheckAll(checked, no) {

            $("input[name='" + no + "']").each(function () {
                this.checked = checked;
            });
            var cbs = $("input[type='checkbox']:checked");
            selectedRows = [];
            cbs.each(function () {
                if (this.id.indexOf('CB_') == -1)
                    return true;
                selectedRows.push({ No: this.value, Name: $(this).siblings().text() });
            });

        }

        function CheckOrUnCheckRow(obj, no, name) {
            if (obj.checked == true) {
                loadSelectGrid(no, name);
            } else {
                remove(no, name);
            }


        }


        //获取选择的项的NO，Name
        function Btn_OK() {
            //发送子流程
            if (selectedRows.length == 0 || $.isArray(selectedRows) == false) {
                alert("请选择需要接收子流程的人员列表");
                return false;
            }
            var selectNos = "";
            $.each(selectedRows, function (i, item) {
                selectNos += item.No + "@" + item.Name + ",";
            });
            //处理发起的流程
            var handler = new HttpHandler("BP.WF.HttpHandler.WF_WorkOpt");
            handler.AddPara("WorkID", GetQueryString("WorkID")); //当前流程的WorkID.
            handler.AddPara("FK_Node", GetQueryString("FK_Node")); //当前节点的
            handler.AddPara("FK_Flow", GetQueryString("FK_Flow")); // 当前流程.
            handler.AddPara("FID", GetQueryString("FID"));
            handler.AddPara("SelectNos", selectNos); //选择的实体编号，名称.
            handler.AddPara("SubFlowMyPK", GetQueryString("SubFlowMyPK"));
            var data = handler.DoMethodReturnString("SubFlowGuid_Send");
            if (data.indexOf("err@") != -1) {
                alert(data);
                return false;
            }
            alert(data);
            return true;
        }
    </script>
</head>
<!--<body>
    <form id="cc">
    <center>
        <div id="showMsg">
        </div>
    </center>
    </form>
</body>-->
<body style="overflow-y:hidden">
    <div class="container-fluid" style="overflow:hidden">
        <div class="row row-margin-top">
            <div id="treeDiv">
                <div class="col-md-6 col-sm-6" style="height:100%">
                    <div id="tree" style="overflow-y:auto;height:500px"></div>
                </div>
                <div class="col-md-6 col-sm-6">
                    <div class="row">
                        <div class="col-md-12">
                            <table id="selectGrid"></table>
                        </div>
                    </div>
                </div>
            </div>
            <div id="listDiv">
                <div class="row">
                    <div class="col-md-12">
                        <table id="listGrid"></table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>
